package com.lagou

import scala.io.StdIn
import scala.util.Random

/*
* 人机猜拳
*
* 1. 选取对战角色
* 2. 开始对战，用户出拳，与对手进行比较，提示胜负信息
* 3. 猜拳结束算分，平局都加一分，获胜加二分，失败不加分
* 4 . 循环对战，当输入“n”时，终止对战，并显示对战结果
* 5. 游戏结束后显示得分
* */
trait Role {
  var score: Int = 0
  var success: Int = 0
  var peace: Int = 0
  var loss: Int = 0

  def showFist(name: String): Unit = {
    //汇总展示
    println(s"$name\t${countScore()}\t$success\t$peace\t$loss")
  }

  def countScore(): Int = {
    //计算分数
    success * 2 + peace * 1 + loss * 0
  }

  def battle_result(result: String): Unit = {
    //统计对局详情
    result match {
      case "success" => success += 1
      case "peace" => peace += 1
      case "loss" => loss += 1
    }
  }
}

class User extends Role {
  var name: String = "游客"
}

class Computer extends Role {
  var name: String = _
}


class Game {
  var role_1: String = _
  var role_2: String = _
  var role_1_index: String = _
  var role_2_index: String = _
  var play_count: Int = 0

  def result_1(): String = {
    //游客的猜拳结果
    role_1_index match {
      case "1" => if (role_2_index == "1") "peace" else if (role_2_index == "2") "loss" else "success"
      case "2" => if (role_2_index == "1") "success" else if (role_2_index == "2") "peace" else "loss"
      case "3" => if (role_2_index == "1") "loss" else if (role_2_index == "2") "success" else "peace"
      case _ => "啥也不是！"
    }
  }

  def result_2(): String = {
    //电脑的猜拳结果
    result_1() match {
      case "success" => "loss"
      case "peace" => "peace"
      case "loss" => "success"
    }
  }
}

class Play {
  var role_1: String = _
  var flag: String = _
  val game = new Game
  val user = new User
  val computer = new Computer

  def init(): Unit = {
    println("请选择对战角色:(1，刘备   2，关羽   3，张飞)")
    var input: String = StdIn.readLine()
    while (!Array("1", "2", "3").contains(input)) {
      println("请选择对战角色:(1，刘备   2，关羽   3，张飞)")
      input = StdIn.readLine()
    }
    role_1 = input match {
      case "1" => "刘备"
      case "2" => "关羽"
      case "3" => "张飞"
    }

    println(s"你选择了与${role_1}对战")

  }

  def translation(string: String): String = {
    string match {
      case "1" => "剪刀"
      case "2" => "石头"
      case "3" => "布"
    }
  }

  def play_(): Unit = {
    println("要开始么？y/n")
    flag = StdIn.readLine()
    //非法输入处理
    while (!Array("y", "n").contains(flag)) {
      println("请输入y或者n！")
      flag = StdIn.readLine()
    }
    //循环执行玩游戏
    while (flag == "y") {
      println("请出拳！1.剪刀  2.石头  3.布")
      //游客出拳的结果
      var index_1: String = StdIn.readLine()
      if (!Array("1", "2", "3").contains(index_1)) {
        println("输入不符合规范，默认出布！")
        index_1 = "3"
      }
      game.role_1_index = index_1
      var string: String = translation(index_1)
      println(s"你出拳：$string")
      //对应的对战角色出拳结果
      println(s"${role_1}出拳!")
      val index_2: String = guess()
      string = translation(index_2)
      println(s"${role_1}出拳：$string")
      game.role_2_index = index_2
      //对出拳结果进行判定
      val result: String = game.result_1()
      result match {
        case "success" => println("结果：恭喜！你赢了！")
        case "loss" => println("结果：你输了，不要灰心，继续加油！")
        case "peace" => println("结果：和局，下次继续努力！")
      }
      user.battle_result(result)
      computer.battle_result(game.result_2())
      game.play_count += 1

      println("是否开始下一轮？y/n")
      var flag_index: String = StdIn.readLine()
      while (!Array("y", "n").contains(flag_index)) {
        println("请输入y或者n！")
        flag_index = StdIn.readLine()
      }
      flag = flag_index
    }
    println("退出游戏！")
    println("-" * 40)
    println(s"$role_1  VS  游客")
    println(s"对战次数${game.play_count}次")
    println()
    println()
    println(s"姓名\t得分\t胜局\t和局\t负局")
    user.showFist(user.name)
    computer.showFist(role_1)

  }


  //计算机随机出拳
  def guess(): String = {
    Array(1, 2, 3)(Random.nextInt(3)).toString
  }

}

object PlayGuess {
  def main(args: Array[String]): Unit = {
    println("-" * 20 + "欢迎进入游戏世界" + "-" * 20)
    println("*" * 40)
    println("*" * 15 + " 猜拳，开始 " + "*" * 15)
    println("*" * 40)
    println()
    println()

    val play = new Play
    play.init()
    play.play_()
  }
}
